<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/28
 * Time: 11:10
 */

namespace app\api\controller;

use think\Db;

class Ali extends Base
{
    protected $config = array(
        'partner' => '2019042964332419',//用不着
        'seller_id' => 'admin@bagepanzi.com',//用不着
        'sign_type' => 'RSA2',// 直接写死了
        'it_b_pay' => '10m',//用不着
        'md5' => 'djqm7budkdlki6a76yasuyb9s1riiu1j',//用不着
        'ali_public' => '////+///++///+/',
        'public_key' => "/++//+/+//+",
        'private_key' => '/++//+/+//+/+++//++++++//++q+///+////+++/+R+dm51VutmZx4TC0GQp3gLd85+YD+LVlkjNyHf3XzCAaCbLyGlU4al9D5moXxbGKFTFkOhDiE8/QRtjuCHy/WMTcbnNuHq/TbAB3p2S3u7EFsHmKEX6ZhsRl/PVN1yARyZDqEmMaEjzGHgZWBAQqXuMtNeECgYAcXL6LPsIQp/B3UagHqpyi0iR/RO5QMIt1wJy1Z5Ltyz2AfKBa3Y9J0xETJMwFq2RJ7CsLwazm9BIZDIVpiGp6lkNdtu9YYy108y3VkIo/EHZDQqpC0DwgB+0NxAyYWy9U2Zl383twdLssoWZxtqh0HKeH9MvqqlDeuiq5+L+4gA=='
    );
    public $body;//描述
    public $subject;//描述
    public $total_amount;//金额
    public $out_trade_no;//订单号
    
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
    }
    
    public function getacc()
    {
        extract(input());
        import('alipay.aop.AopClient', EXTEND_PATH);
        import('alipay.aop.request.AlipaySystemOauthTokenRequest', EXTEND_PATH);
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2019042964332419';
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAz7etbhvVXA4hCn5r/4Xnzu2uZPmJeBGHsSWrXG6XCjbMTuHGhAhGGboNmWX0+e6Ro6E86lWEFgtrUkQPyFoO6D4uEWQ4/f+7WrYZ88C2Zrm4Y7rqFh3uQPpvpalWWmI3B+ORw+zqx2vB2mn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CgYEAhIwkIyc3VhAz2B4NTofPUDfgQX/CErcOIJCbq2Xp7SntQFGOe3/e2/bpkCudmvyOggn3Z6QfYwoKw8HRa1srpTi1ur4+rBG1bHS+JXb7gXAjIQxHe9cWZA4kioGlw2JptJy0WMukLbn6Z0MnrW5w7tfQV1peS7TU7tCeVOeOrQkCgYBBJGELW8yOnXOrIxgQzdIpACmjmAZK97uLoI8FwyFh9sGX+h24sEDFlom327be7d9hjvyj6FB++tO+x85kHti2ZBJzhHJxgAMClWD5pt/rYOmVFqq/Zas69Lfecck7bxvibjlw7Bnvuj+6aIoW0ZhEi1jdJUn13mqqNLJte45Luw==';
        $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmQrUzE+dANPCKu1Jvr8O9j7+X0AfPd5p9flxEx6rQEVF7vZDuyf7jyXOdMRP7+bxbhyhMabgpj+nuMBvHiGl7NEYO0ugtZtyo0eAy2UoMqPYw3gLvNxgKspOLj6WDZa0Qw0ExFX235+VZSfRwY+ZDEsRCXAh2lcs5W66O3Y6yF72CdscTfKBGLkhP6hIj9rGXNBT9olu1UmfLOuntiz+3Y6dbODZqLzmzVKy+MbRIt2ZNyje+n+wrCemwDucbkvadoq0/4JCnwe8CeY7SFb630nvoTe2kN9kuH6W/jiz7dEf6N75hMfiHZ5ff4bjpOYPvMEFbKwp23N50Db4Kvhz1wIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        $actoken = $result->alipay_system_oauth_token_response->access_token;
        $user = $this->getdenglu($actoken);
        if($user === false){
            return json(['status'=>100,'msg'=>'刷新重试']);
        }else{
            return json(['status'=>200,'msg'=>$user]);
        }
    }
    public function getdenglu($actoken)
    {
        import('alipay.aop.AopClient', EXTEND_PATH);
        import('alipay.aop.request.AlipayUserInfoShareRequest', EXTEND_PATH);
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2019042964332419';
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAz7etbhvVXA4hCn5r/4Xnzu2uZPmJeBGHsSWrXG6XCjbMTuHGhAhGGboNmWX0+e6Ro6E86lWEFgtrUkQPyFoO6D4uEWQ4/f+7WrYZ88C2Zrm4Y7rqFh3uQPpvpalWWmI3B+ORw+zqx2vB2mnJbdPSLujy6m69iGEzf8zXyEJ5F0HQNXG1ajP86jMSdLgp8fgiKa+DnGzcgCFZ4o2fIARU22hJX2zV7pfxR24EcvF9UU6CgCxfdclfXLWCsijBCuQnIFlujkZpuz+h4TWts3IFXiNFcY5tPvf++lzHfglZ08y/+1Wayq7Q9w6wmxvXTwj1buYVfLkijSMsg198DB08OwIDAQABAoIBAFmVxUWs2Ws3aMam2Zs9vElBtyozeTeAj/412d8mjxvHoHwQ4dZF3z3vQTg8gdhdLRDeVHBYa3w6dpHnasslFDLTxniXugDKD5rC2RSIs+JADSduENFzHTOHDynqi6S1oThUzKQshOF3NwclAsvMRGUY8NNs6W9tRJHE3fevu5vsbhjvM9tvZfQpmnNbnwE42rwlEnAm05THX6ZM3rWcPPKRGLNFUWyRhHpM7GAZXpYc0FJdRkxU+7ZDMIXc6raCpJyUxCmcDGbtpbeb/qQQ/2q2J6hD615V4qFY36hIb0GmhdC4skGF9JXYn+kE7fQAN6aeKyJYbbS/srmVpwuVP1ECgYEA+AX2Bp5zkZOaxACYRwazENZ0WXCizJIWetOEjXS+iQ0Z31qE+MdlMf9kbJIDct/H0Omxixcd4XvvJsJF2RhQynlMBeqI9azwcNbCeoBD+N38T7m0jzCn/LI+SqWoTJ3nlpaNHRKCSxM1/C1kuX+0lhFbmiGPS7p62JFCtyAN1AMCgYEA1mXeWBYN2GGRMOjaMD3BIfswPI4Vgrbrz34SPBjtic7r+o8G4y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ion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayUserInfoShareRequest ();
        $result = $aop->execute ( $request , $actoken );
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            $user = $result->alipay_user_info_share_response;
            $user = (array)$user;
            return $user;
        } else {
            return false;
        }
    }
    public function getsignali()
    {
        import('alipay.aop.AopClient', EXTEND_PATH);
        $aop = new \AopClient ();
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAz7etbhvVXA4hCn5r/4Xnzu2uZPmJeBGHsSWrXG6XCjbMTuHGhAhGGboNmWX0+e6Ro6E86lWEFgtrUkQPyFoO6D4uEWQ4/f+7WrYZ88C2Zrm4Y7rqFh3uQPpvpalWWmI3B+ORw+zqx2vB2mnJbdPSLujy6m69iGEzf8zXyEJ5F0HQNXG1ajP86jMSdLgp8fgiKa+DnGzcgCFZ4o2fIARU22hJX2zV7pfxR24EcvF9UU6CgCxfdclfXLWCsijBCuQnIFlujkZpuz+h4TWts3IFXiNFcY5tPvf++lzHfglZ08y/+1Wayq7Q9w6wmxvXTwj1buYVfLkijSMsg198DB08OwIDAQABAoIBAFmVxUWs2Ws3aMam2Zs9vElBtyozeTeAj/412d8mjxvHoHwQ4dZF3z3vQTg8gdhdLRDeVHBYa3w6dpHnasslFDLTxniXugDKD5rC2RSIs+JADSduENFzHTOHDynqi6S1oThUzKQshOF3NwclAsvMRGUY8NNs6W9tRJHE3fevu5vsbhjvM9tvZfQpmnNbnwE42rwlEnAm05THX6ZM3rWcPPKRGLNFUWyRhHpM7GAZXpYc0FJdRkxU+7ZDMIXc6raCpJyUxCmcDGbtpbeb/qQQ/2q2J6hD615V4qFY36hIb0GmhdC4skGF9JXYn+kE7fQAN6aeKyJYbbS/srmVpwuVP1ECgYEA+AX2Bp5zkZOaxACYRwazENZ0WXCizJIWetOEjXS+iQ0Z31qE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GLkhP6hIj9rGXNBT9olu1UmfLOuntiz+3Y6dbODZqLzmzVKy+MbRIt2ZNyje+n+wrCemwDucbkvadoq0/4JCnwe8CeY7SFb630nvoTe2kN9kuH6W/jiz7dEf6N75hMfiHZ5ff4bjpOYPvMEFbKwp23N50Db4Kvhz1wIDAQAB';
        $data = ["apiname"=>"com.alipay.account.auth",
            "method"=> "alipay.open.auth.sdk.code.get",
            "app_id"=> "2019042964332419",
            "app_name"=> "mc",
            "biz_type"=> "openservice",
            "product_id"=> "APP_FAST_LOGIN",
            "scope"=> "kuaijie",
            "target_id"=> "kkkkk091125",
            "auth_type"=> "AUTHACCOUNT",
            "sign_type"=> "RSA2",
            "pid"=>"2088431122333723"];
        $a = 'apiname=com.alipay.account.auth&method=alipay.open.auth.sdk.code.get&app_id=2019042964332419&app_name=mc&biz_type=openservice&pid=2088431122333723&product_id=APP_FAST_LOGIN&scope=kuaijie&target_id=kkkkk091125&auth_type=AUTHACCOUNT&sign_type=RSA2';
        $sign = $aop->generateSign($data,'RSA2');
        $sign = urlencode($sign);
        $a .= '&sign=' . $sign;
        return json(['status'=>200,'msg'=>$a]);
    }

    /**
     * 获取人脸识别的biztoken
     * @return \think\response\Json
     */
    public function getbiztoken(){
        header("Content-type: text/html; charset=utf-8");
        extract(input());
        $signs = $this->gen_sign('eTo3MEEYiP72FW9Ic_0Chrtem1L89VHK', 'QcY7x4tNgvw5S_tgWUO78NwdFr_yxACN', 0);
        dump($signs);
        $body_json = array(
            "sign"=> $signs,
            "sign_version"=> "hmac_sha1",
            "liveness_type"=> "meglive",
            "comparison_type"=> 1,
            "idcard_name"=> "麻威威",
            "idcard_number"=> "412702198901153115",
            "multi_oriented_detection"=> 1,
        );
        $body = json_encode($body_json);
        $header = array(
            'Accept:application/json',
            'Content-Type:application/json;charset=utf-8'
        );
//        $url = 'https://openapi.faceid.com/face/v1.2/sdk/get_biz_token';
        $url = 'https://api.megvii.com/faceid/v3/sdk/get_biz_token';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($result, true);
        dump($res);
    }
    protected function gen_sign($apiKey, $apiSecret, $expired){
        $rdm = rand();
        $current_time = time();
        $expired_time = $current_time + $expired;
        $srcStr = "a=%s&b=%d&c=%d&d=%d";
        $srcStr = sprintf($srcStr, $apiKey, $expired_time, $current_time, $rdm);
        $sign = base64_encode(hash_hmac('SHA1', $srcStr, $apiSecret, true).$srcStr);
        return $sign;
    }

}